package com.canyou.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.canyou.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

/**
 * 阿里oss文件上传
 *
 * @author fy
 * @date 20/08/31 11:39
 */
@Service
@Slf4j
public class AliOssService {

    @Autowired
    private AliOssProperties config;

    /**
     * 创建ossClient
     *
     * @return
     */
    private OSS getOssClient() {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = config.getEndpoint();
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，
        // 请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = config.getAccessKeyId();
        String accessKeySecret = config.getAccessKeySecret();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    /**
     * 文件上传到阿里云oss
     *
     * @param file
     * @param fileName
     * @return
     */
    public PutObjectResult upload(File file, String fileName) {
        OSS ossClient = getOssClient();
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(config.getBucket(), fileName, file);
        // 上传文件
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        ossClient.shutdown();
        return putObjectResult;
    }

    /**
     * 文件上传到阿里云oss
     *
     * @param stream
     * @param fileName
     * @return
     */
    public PutObjectResult upload(InputStream stream, String fileName) {
        OSS ossClient = getOssClient();

        String bucket = config.getBucket();
        String objectName = fileName;

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, objectName, stream);

        // 上传文件
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        ossClient.shutdown();
        return putObjectResult;
    }

    /**
     * 公共读Object
     *
     * @param objectName
     * @return
     */
    public String publicAccessUrl(String objectName) {
        return StringUtils.format("https://{}.{}/{}", config.getBucket(), config.getEndpoint(), objectName);
    }

    /**
     * 使用签名URL临时授权
     *
     * @param objectName
     * @return
     */
    public String privateAccessUrl(String objectName) {
        OSS ossClient = getOssClient();
        // 设置URL过期时间
        Date expiration = new Date(System.currentTimeMillis() + config.getExpiration() * 1000);
        // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(config.getBucket(), objectName, expiration);

        // 关闭OSSClient
        ossClient.shutdown();
        // 构建访问路径
        return StringUtils.format("{}://{}{}", url.getProtocol(), url.getHost(), url.getFile());
    }


}
